package com.wanli.agent.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wanli.agent.R;
import com.wanli.agent.bean.WithdrawListBean;
import com.wanli.agent.event.WithdrawDataEvent;
import com.wanli.agent.homepage.WithdrawDetailsActivity;
import com.wanli.agent.utils.DateTimeUtil;
import com.wanli.agent.utils.NumberFormatUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 提现记录列表
 */
public class WithdrawListAdapter extends RecyclerView.Adapter<WithdrawListAdapter.ViewHolder> {

    private List<WithdrawListBean.DataBean.ItemListBean> listsBeans;
    private Context context;

    public WithdrawListAdapter(List<WithdrawListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_withdraw_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        WithdrawListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        //0-服务商已申请,1-财务已初审,2-财务转账中,3-服务商已撤销,4-财务已拒绝,9-转账成功10-转账失败
        if (dataBean.getStatus() == 0) {
            holder.tvName.setText("服务商已申请");
        } else if (dataBean.getStatus() == 1) {
            holder.tvName.setText("财务已初审");
        } else if (dataBean.getStatus() == 2) {
            holder.tvName.setText("财务转账中");
        } else if (dataBean.getStatus() == 3) {
            holder.tvName.setText("服务商已撤销");
        } else if (dataBean.getStatus() == 4) {
            holder.tvName.setText("财务已拒绝");
        } else if (dataBean.getStatus() == 9) {
            holder.tvName.setText("成功");
        } else {
            holder.tvName.setText("成功");
        }
        holder.tvTime.setText("提现日期：" + DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));
        String money = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(dataBean.getMoney()) / 100);
        holder.tvMoney.setText(money);
        holder.tvAgent.setText(dataBean.getAgent_name());
        holder.itemView.setOnClickListener(v -> {
            EventBus.getDefault().postSticky(new WithdrawDataEvent(dataBean));
            Intent intent = new Intent(context, WithdrawDetailsActivity.class);
            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_agent)
        TextView tvAgent;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
